package com.zhkj.txg.module.member.popup

import android.content.Context
import android.view.View
import android.widget.EditText
import com.zhkj.lib.utils.ToastUtil
import com.zhkj.txg.R
import razerdp.basepopup.BasePopupWindow

class MemberInvitePopupWindow(
    context: Context,
    private val confirm: (inviteCode: String) -> Unit
) : BasePopupWindow(context) {

    init {
        setAdjustInputMethod(false)
    }

    private var hasInvite: View? = null
    private var hasNotInvite: View? = null
    private var editText: EditText? = null

    override fun onCreateContentView(): View = createPopupById(R.layout.popup_member_invite)
    override fun onViewCreated(contentView: View?) {
        editText = findViewById(R.id.etInviteCode)
        hasInvite = findViewById(R.id.tvHasInvite)
        hasNotInvite = findViewById(R.id.tvHasNotInvite)
        hasInvite?.setOnClickListener { setHasInvite(true) }
        hasNotInvite?.setOnClickListener { setHasInvite(false) }
        setHasInvite(true)
        findViewById<View>(R.id.tvConfirm).setOnClickListener {
            if (hasInvite?.isSelected == true && editText?.text.isNullOrEmpty()) {
                ToastUtil.show(context.getString(R.string.input_invite_id))
            } else confirm(editText?.text?.toString() ?: "")
        }
        findViewById<View>(R.id.tvCancel).setOnClickListener { dismiss() }
    }

    private fun setHasInvite(invite: Boolean) {
        hasInvite?.isSelected = invite
        hasNotInvite?.isSelected = !invite
        editText?.isEnabled = invite
        editText?.hint = context.getString(if (invite) R.string.input_invite_id else R.string.default_platform_id)
        if (!invite) editText?.setText("")
    }
}